﻿/*
This file is under Microsoft Public License (MS-PL)
Copyright Yohann MARTEL.  All rights reserved.
Codeplex: http://amf.codeplex.com
AMF Website: http://www.ymartel.com/aspnet-mobile-framework/
*/
using System;
using System.ComponentModel;
using System.Web.UI;

namespace Mobile.WebControls
{
    [ParseChildren(true)]
    [ToolboxData("<{0}:MobilePage runat=server></{0}:MobilePage>")]
    public class MobilePage : Div
    {
        #region Properties
        [Browsable(false), DefaultValue(null), PersistenceMode(PersistenceMode.InnerProperty),
         TemplateContainer(typeof(Header))]
        public ITemplate Header { get; set; }

        [Category("Header")]
        public string HeaderId { get; set; }

        [Category("Header")]
        public string HeaderName { get; set; }

        [Category("Header")]
        public DataTheme HeaderTheme { get; set; }

        [Category("Header")]
        public string HeaderCssClass  { get; set; }

        [Category("Header")]
        public DataPosition HeaderPosition { get; set; }

        public string Id { get; set; }

        /*
         * Will be implemented soon 
         * 
        [Category("Header")]
        public bool AddHeaderBackButton { get; set; }

        [Category("Header")]
        public string HeaderBackButtonText { get; set; }
         */ 

        [Browsable(false), DefaultValue(null), PersistenceMode(PersistenceMode.InnerProperty),
         TemplateContainer(typeof(Content))]
        public ITemplate Content { get; set; }

        [Category("Content")]
        public string ContentId { get; set; }

        [Category("Content")]
        public string ContentName { get; set; }

        [Category("Content")]
        public DataTheme ContentTheme { get; set; }

        [Category("Content")]
        public string ContentCssClass { get; set; }

        [Browsable(false), DefaultValue(null), PersistenceMode(PersistenceMode.InnerProperty),
         TemplateContainer(typeof(Footer))]
        public ITemplate Footer { get; set; }

        [Category("Footer")]
        public string FooterId { get; set; }

        [Category("Footer")]
        public string FooterName { get; set; }

        [Category("Footer")]
        public DataTheme FooterTheme { get; set; }

        [Category("Footer")]
        public FooterDisplay FooterDisplay { get; set; }

        [Category("Footer")]
        public string FooterCssClass { get; set; }

        [Category("Footer")]
        public DataPosition FooterPosition { get; set; }

        [Category("Footer")]
        public string FooterPersistentId { get; set; }

        [Category("Appearance")]
        public bool DisplayInFullScreen { get; set; }

        public override ControlCollection Controls
        {
            get
            {
                EnsureChildControls();
                return base.Controls;
            }
        }
        #endregion

        #region Methods
        private void CheckFullScreenState()
        {
            if (DisplayInFullScreen)
            {
                FooterPosition = DataPosition.Fixed;
                HeaderPosition = DataPosition.Fixed;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            CheckFullScreenState();
            if (Header != null)
            {
                Header header = new Header();
                header.ID = HeaderId;
                header.Name = HeaderName;
                header.CssClass = HeaderCssClass;
                header.Theme = HeaderTheme;
                header.Position = HeaderPosition;
                /*
                header.AddBackButton = AddHeaderBackButton;
                header.BackButtonText = HeaderBackButtonText;*/
                Header.InstantiateIn(header);
                Controls.Add(header);
            }

            if (Content != null)
            {
                Content content = new Content();
                content.ID = ContentId;
                content.Name = ContentName;
                content.CssClass = ContentCssClass;
                content.Theme = ContentTheme;
                Content.InstantiateIn(content);
                Controls.Add(content);
            }

            if (Footer != null)
            {
                Footer footer = new Footer();
                footer.ID = FooterId;
                footer.Name = FooterName;
                footer.CssClass = FooterCssClass;
                footer.Theme = FooterTheme;
                footer.Display = FooterDisplay;
                footer.Position = FooterPosition;
                footer.PersistentId = FooterPersistentId;
                Footer.InstantiateIn(footer);
                Controls.Add(footer);
            }
        }
        #endregion

        #region Render Methods
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            writer.AddAttribute(MetadataAttribute.DataRole, DataRole.Page);
            if (DisplayInFullScreen)
            {
                writer.AddAttribute(MetadataAttribute.DataFullscreen, Boolean.TrueString.ToLower());
            }
            if (!String.IsNullOrEmpty(Id))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Id, Id);
            }
        }
        #endregion
    }
}
